package com.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.action.UserAction;
import com.bean.UserBean;
import com.exception.DBException;
import com.exception.UserException;
import com.util.HandleInfo;
import com.util.Regex;
import com.util.StateInfo;

public class UserModifyServlet extends HttpServlet
{

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		String option = request.getParameter("option");
		if (null == option)
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		if ("edit".equals(option))
		{
			String userid = request.getParameter("usermodifyid");
			if (null == userid)
			{
				HandleInfo.handle(request, response,
						StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
			UserBean userBean = new UserBean();
			userBean.setUserID(userid);
			try
			{
				new UserAction().modifyUserInfo(request, response, userBean);
			}
			catch (DBException e)
			{
				HandleInfo.handle(request, response, e.getMessage());
			}
			catch (UserException e)
			{
				HandleInfo.handle(request, response, e.getMessage());
			}
		}
		if ("commit".equals(option))
		{
			// confirm the user infomation
			String userid = request.getParameter("userid");
			String firstname = request.getParameter("firstname");
			String lastname = request.getParameter("lastname");
			String age = request.getParameter("age");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
			String gender = request.getParameter("gender");
			String remark = request.getParameter("remark");
			// validate the information
			if (null == userid || null == firstname || null == lastname
					|| null == age || null == email || null == phone
					|| null == remark)
			{
				HandleInfo.handle(request, response,
						StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
			if ("".equals(userid) || "".equals(firstname)
					|| "".equals(lastname) || "".equals(email))
			{
				HandleInfo.handle(request, response,
						StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
			if (userid.length() != 8 || firstname.length() > 30
					|| lastname.length() > 30 || email.length() > 50
					|| phone.length() > 13)
			{
				HandleInfo.handle(request, response,
						StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
			if (null != gender)
			{
				if (!"male".equals(gender) && !"female".equals(gender))
				{
					HandleInfo.handle(request, response,
							StateInfo.INNER_CONFIRM_FAIL);
					return;
				}
			}
			if (null == gender)
			{
				gender = "";
			}
			if (!Regex.checkUserID(userid) || !Regex.checkName(firstname)
					|| !Regex.checkName(lastname) || !Regex.checkEmail(email))
			{
				HandleInfo.handle(request, response,
						StateInfo.INNER_CONFIRM_FAIL);
				return;
			}
			if (!"".equals(phone))
			{
				if (!Regex.checkPhone(phone))
				{
					HandleInfo.handle(request, response,
							StateInfo.INNER_CONFIRM_FAIL);
					return;
				}
			}
			if (!"".equals(age))
			{
				if (!Regex.checkAge(age))
				{
					HandleInfo.handle(request, response,
							StateInfo.INNER_CONFIRM_FAIL);
					return;
				}
			}

			// generate a bean
			UserBean userBean = new UserBean();
			userBean.setUserID(userid);
			userBean.setFirstname(firstname);
			userBean.setLastname(lastname);
			userBean.setAge(age);
			userBean.setGender(gender);
			userBean.setEmail(email);
			userBean.setRemark(remark);
			userBean.setPhone(phone);
			try
			{
				new UserAction().modifyUser(request, response, userBean);
			}
			catch (DBException e)
			{
				HandleInfo.handle(request, response, e.getMessage());
				return;
			}
			catch (UserException e)
			{
				HandleInfo.handle(request, response, e.getMessage());
				return;
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doGet(request, response);
	}

}
